#!/bin/sh
thisDir=`dirname $0`
thisFile=`basename $0`
userName=`/usr/bin/whoami`

if [ "$userName" != "root" ]
then
        echo "You must be root to install packages."
        echo -e "Please enter root \c"
        su root -c "$thisDir/$thisFile" "$*"
        exit 1
fi

if [ $# -lt "1" ]; then
    echo usage:
    echo "    $thisDir/$thisFile AppsDir"
    echo "    Where AppsDir is the software Installation binaries Directory "
    echo "    on the ftp Server NOT including its hostname or IP address"
    echo "    i.e. $thisDir/$thisFile /home/somebody/someSOftware"
    exit 1
fi

echo installing, please wait...


#stop the running software
/someSoftware/stopService
echo sleep 10 seconds to wait process stop
sleep 10
cd /opt

packageServer=x.y.z.p
ftpUserName=user
ftpPassword=password
appsFolder=$1
ftp -ivn >ftp.output <<ftpcheckcommands
open $packageServer
user $ftpUserName $ftpPassword
bin
cd $appsFolder
by
ftpcheckcommands

if  grep -qi "250 Directory successfully changed"  ftp.output 
then
    rm -f ftp.output 
    echo "Test  Application Directory on the server success"
else
    echo "The supplied software directory on the server is not correct, please verify"
    rm -f ftp.output
    exit 1
fi 

#Delete the old installing files
find -maxdepth 1 -name "*.tgz.sh*" -type f -exec rm -f {} \;
rm -rf someDirectory


ftp -ivn <<ftpcommand
open $packageServer
user $ftpUserName $ftpPassword
bin
cd $appsFolder
mget *
by
ftpcommand

#Stat install files attributes. Assume the name of install files like "xxx.V1.1.1.1b6.tgz.sh.b6"
#V1.1.1.1b6 is the big version.
xxxInstallFile=`ls | grep "xxx.*.tgz.sh" `
yyyInstallFile=`ls | grep "yyy.*.tgz.sh" `

echo xxxInstallFile is $xxxInstallFile
echo yyyInstallFile is $yyyInstallFile

xxxVer=`echo $xxxInstallFile | sed -e 's/.tgz.sh.*//'| sed -e 's/xxx.//'`
yyyVer=`echo $yyyInstallFile | sed -e 's/.tgz.sh.*//'| sed -e 's/yyy.//'`

echo xxxVer is $xxxVer
echo yyyVer is $yyyVer


chmod a+x $xxxInstallFile
chmod a+x $yyyInstallFile

./installSoftware -i $xxxInstallFile
./installSoftware -i $yyyInstallFile

#Other operations
sync
shutdown -r now
